package  simple_riscv
import spinal.core.fiber.Handle
import spinal.core._
import scala.collection.mutable.ArrayBuffer
import scala.reflect.ClassTag


trait Service{

}

trait Plugin extends Area with Service{
    val frame = Handle[Framework]()
    val configHandleArr = ArrayBuffer[Handle[_]]()
    def create = new {
        def config[T](body: => T): Handle[T] = {
            val ret = Handle{
                frame.configLock.await() // sleep
                val v = frame.rework(body)
                v
            }
            configHandleArr += ret
            ret
        }
        def early[T](body: => T): Handle[T] = {
            val ret = Handle{
                frame.earlyBuildLock.await() // sleep
                val v = frame.rework(body)
                v
            }
            ret
        }
        def late[T](body: => T): Handle[T] = {
            val ret = Handle{
                frame.lateBuildLock.await() // sleep
                val v = frame.rework(body)
                v
            }
            ret
        }
    }

    def getService[T <: Service : ClassTag] = frame.getService[T]
}
